Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
How to constrain a generic type in Flow to allow compare operators like < or lessThen?

Consider the following generic function in Flow:

function lessThen<T>(a: T, b: T) {
  return a < b;
}

The intention here is to instantiate lessThen with a type that allows the < operator like number or string.

In this form Flow complains about it:

return a < b;
            ^ Cannot compare `T` [1] to `T` [2]. [invalid-compare]
References:
1: function lessThen<T>(a: T, b: T) {
                           ^ [1]
1: function lessThen<T>(a: T, b: T) {
                                 ^ [2]

So I suppose I need to constrain T in lessThen to support <. But how do I do that? I was looking to something like build-in type CanUseCompareOperators so I can write:

function lessThen<T: CanUseCompareOperators>(a: T, b: T) {
  return a < b;
}

but I cannot find any.

Although in this toy example I can drop the generics and let Flow infere the type, in the real code I need this to work for a methods in a generic class when dropping generics is not an option like in:

class Foo<Key, Value> {
  binarySearch() {
    // code that uses < and > to compare keys
  }
}

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda